package manager.entitycreator;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import util.DateUtil;
import entity.Course;

/**
 * Implements methods that describe how to extract course information from the
 * given ResultSet.
 * 
 * @author sheryl.tay.2012, mohameds.2012
 * 
 */
public class CourseCreator implements EntityCreator<Course> {

	@Override
	public Course newEntityFromResultSet(ResultSet rs) throws SQLException {
		return newCourseFromResultSet(rs);
	}

	/**
	 * Creates a new Course entity from the given result set.
	 * 
	 * @param rs
	 *            result set containing course information
	 * @return a new Course entity with information extracted from the result
	 *         set
	 * @throws SQLException
	 */
	static Course newCourseFromResultSet(ResultSet rs) throws SQLException {

		String courseCode = rs.getString("course_courseCode");
		String title = rs.getString("course_title");
		String description = rs.getString("course_description");
		String school = rs.getString("course_school");
		Date examDate = DateUtil.convertToDate(rs.getString("course_examDate"),
				DateUtil.MYSQL_DATE_PATTERN);
		Date examStartTime = DateUtil.convertToDate(
				rs.getString("course_examStartTime"),
				DateUtil.MYSQL_TIME_PATTERN);
		Date examEndTime = DateUtil
				.convertToDate(rs.getString("course_examEndTime"),
						DateUtil.MYSQL_TIME_PATTERN);

		Course course = new Course(courseCode, title, description, school,
				examDate, examStartTime, examEndTime);

		return course;
	}

}
